/* -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-
vim: ts=4 noet ai */

/**
	ScrollMeter -- Measure how far you scrolled today

	Copyright © 2016  Desktopd Project

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

	@license GPL-3.0+
	@file
*/

'use strict';

// SDK modules
const _sdkTabs = require ('sdk/tabs');
const _sdkSelf = require ('sdk/self');
const _sdkUrl = require ('sdk/url');
const _sdkPageMod = require ('sdk/page-mod');
const _sdkPrivateBrowsing = require ('sdk/private-browsing');
const _sdkPanel = require ('sdk/panel');
const _sdkSimplePrefs = require ('sdk/simple-prefs');
const {ToggleButton: _sdkToggleButton} = require ('sdk/ui/button/toggle');


// Global states: counters for this session, in meters
const _normalCounters = Object.create (null);
const _privateCounters = Object.create (null); // Must not be saved

// button
const _button = _sdkToggleButton ({__proto__: null
	,id: 'panel-button'
	,label: 'ScrollMeter'
	,icon: './button.svg'
});

// panel
const _panel = _sdkPanel.Panel ({__proto__: null
	,contentURL: './panel.xhtml'
	,onHide () {
		const button = _button;
		button.state ('window', {checked: false});
	}
});

_button.on ('change', state => {
	const panel = _panel, button = _button;
	
	if (state.checked) {
		const sdkSimplePrefs = _sdkSimplePrefs;
		const sdkTabs = _sdkTabs;
		const sdkPrivateBrowsing = _sdkPrivateBrowsing;
		const isPrivate = sdkPrivateBrowsing.isPrivate (sdkTabs.activeTab);
		
		const counters = isPrivate ? _privateCounters : _normalCounters;
		const ratio = 1 / sdkSimplePrefs.prefs.unit;
		panel.port.emit ('show', [counters, isPrivate, ratio]);
		panel.show ({position: button});
	}
});


const openPreferences = () => {
	const sdkTabs = _sdkTabs, sdkSelf = _sdkSelf;
	
	sdkTabs.open ({__proto__: null
		,url: 'about:addons'
		,onOpen (aSdkTab) {
			aSdkTab.once ('ready', () => {
				aSdkTab.attach ({__proto__: null
					,contentScript: `
'use strict';
AddonManager.getAddonByID(self.options.addonId, addon => {
	unsafeWindow.gViewController.commands.cmd_showItemDetails
		.doCommand (addon, true);
});
				`
					,contentScriptOptions: {addonId: '' + sdkSelf.id}
				});
			});
		}
	});
};

_panel.port.on ('openPreferences', () => {
	openPreferences ();
});

const getOrigin = aUri => {
	const sdkUrl = _sdkUrl;
	return '' + (sdkUrl.URL ((aUri || '') + '').host || '');
};

_sdkPageMod.PageMod ({__proto__: null
	,include: '*' // every site that uses http://, https:// or ftp://
	,contentScriptFile: './measure.js'
	,contentScriptWhen: 'ready'
	//,attachTo: ['existing', 'frame']
	,onAttach (aSdkWorker) {
		aSdkWorker.port.on ('scroll', length => {
			const sdkPrivateBrowsing = _sdkPrivateBrowsing;
			const uri = (aSdkWorker.tab.url || '') + '';
			const isPrivate = !!(sdkPrivateBrowsing.isPrivate (aSdkWorker.tab));
			const origin = getOrigin (uri);
			const counters = isPrivate ? _privateCounters : _normalCounters;
			if (!counters[origin]) {
				counters[origin] = 0;
			}
			
			counters[origin] += length;
		});
	}
});

